<?php
/**
 * 权限管理
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-6-17
 * Time: 下午2:37
 */
namespace app\modules\content\controllers;

use app\libs\Pager;
use yii;
use app\libs\ApiControl;
use app\modules\content\models\Evaluation;
use app\libs\GetData;

class EvaluationController extends ApiControl
{
    public $enableCsrfValidation = false;

    /**
     * @Title  [index]
     * @Author Don
     * @Date   2021-04-12
     * @return [type]
     */
    public function actionIndex()
    {
        $data = Yii::$app->db->createCommand("select * from {{%evaluation}} order by id desc")->queryAll();
        // f_d($data);
        return $this->render('index', ['data' => $data]);
    }

    /**
     * @Title  [添加评价]
     * @Author Don
     * @Date   2021-04-12
     * @return [type]
     */
    public function actionAdd()
    {
        if (!$_POST) {
            $id = Yii::$app->request->get('id', '');
            if ($id == '') {
                return $this->render('add');
            } else {
                $data = Yii::$app->db->createCommand("select * from {{%evaluation}} where id=" . $id)->queryOne();
                return $this->render('add', ['data' => $data]);
            }
        } else {
            $getdata = new GetData();
            $postion = 'evaluation';
            $must = array();
            $data = $getdata->PostData($must, 'evaluation');
            
            if ($data['id'] == '') {
                $re = Yii::$app->db->createCommand()->insert("{{%evaluation}}", $data)->execute();
            } else {
                $model = new Evaluation();
                $re = $model->updateAll($data, 'id=:id', array(':id' => $data['id']));
            }
            if ($re) {
                $this->redirect('index');
            } else {
                echo '<script>alert("数据添加\修改失败，请重试");history.go(-1);</script>';
                die;
            }
        }
    }

    /**
     * @Title  [删除]
     * @Author Don
     * @Date   2021-04-12
     * @return [type]
     */
    public function actionDel()
    {
        $id = Yii::$app->request->get('id', '');
        $re = Evaluation::deleteAll("id=:id", array(':id' => $id));
        if ($re) {
            echo true;
        }

    }
}